/*
 * 文件名称: FixFeedUidBug.java Copyright 2011-2016 Ximalaya All right reserved.
 */
package com.zxy.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.TreeMap;

/**
 * @author Bird Zhang
 */
public class FixFeedUidBug {

  public static void main(String[] args) {

    // 1. 获得tb_comment**的node分布表
    // String host = "192.168.1.161";
    // String user = "naliworld";
    // String password = "password!";
    //String tbName = "tb_comment";
    String host = args[0];
    String user = args[1];
    String password = args[2];
    final String dbName = "chaos";
    String tbName = args[3];
    TreeMap<Long, String> commentNodeTree = QueryIbatorNode.genNodes(host, user, password, dbName, tbName);

    // 2.遍历tb_comment0-tb_comment127
    for (int i = 0; i <= 127; i++) {
      String commentTBName = tbName + i;
      String jdbcUrl = "jdbc:mysql://" + host + ":3306/" + dbName + "?" + "user=" + user + "&password=" + password
          + "&useUnicode=true&characterEncoding=utf8";
      Statement statement1 = null;
      Statement statement2 = null;
      Statement statement3 = null;
      ResultSet resultSet1 = null;
      ResultSet resultSet2 = null;
      Connection conn = null;
      try {
        Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
        System.out.println("成功加载MySQL驱动程序");
        // 一个Connection代表一个数据库连接
        conn = DriverManager.getConnection(jdbcUrl);
        // Statement里面带有很多方法，比如executeUpdate可以实现插入，更新和删除等
        String sql = "select id,feed_id from " + dbName + "." + commentTBName;
        System.out.println("SQL:" + sql);
        statement1 = conn.createStatement();
        resultSet1 = statement1.executeQuery(sql);
        while (resultSet1.next()) {
          Long id = resultSet1.getLong(1);
          Long feed_id = resultSet1.getLong(2);

          String tb_feed_suffix = String.valueOf(feed_id / 420);
          String feedTBName = "tb_feed" + tb_feed_suffix;
          sql = "select uid from " + dbName + "." + feedTBName + " where id = " + feed_id;
          System.out.println("SQL:" + sql);
          statement2 = conn.createStatement();
          resultSet2 = statement2.executeQuery(sql);
          while (resultSet2.next()) {
            Long feed_uid = resultSet2.getLong(1);

            sql = "UPDATE `chaos`.`" + commentTBName + "` SET feed_uid = " + feed_uid + " WHERE id = " + id;
            System.out.println("SQL:" + sql);
            statement3 = conn.createStatement();
            statement3.execute(sql);
          }
          // QueryIbatorNode.getTableSuffix(feed_id, commentNodeTree);
          // System.out.println("id=" + id + ",feed_id=" + feed_id);
          // ret.put(resultSet.getLong(1), resultSet.getString(2).replaceAll(tbName, ""));
        }

      } catch (SQLException e) {
        System.out.println("MySQL操作错误");
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
      }
    }
  }
}
